<template>
  <el-dialog
    v-el-drag-dialog
    :title="type === 'add' ? '新增车辆配置' : '编辑车辆配置'"
    :visible.sync="visible"
    width="700px"
    @closed="reset"
  >
    <el-form ref="dataForm" class="form" :rules="rules" :model="temp" label-position="right" label-width="80px">
      <el-form-item label="车辆" prop="truckCode">
        <el-select v-model="temp.truckCode" placeholder="请选择" clearable>
          <el-option
            v-for="item in vehicleList"
            :key="item.truckId"
            :label="item.truckCode"
            :value="item.truckCode"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="司机" prop="driverUserIds">
        <el-select v-model="temp.driverUserIds" multiple collapse-tags placeholder="请选择" clearable>
          <el-option
            v-for="item in driverList"
            :key="item.userId"
            :label="item.userName"
            :value="item.userId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="押运员" prop="escortUserIds">
        <el-select v-model="temp.escortUserIds" multiple collapse-tags placeholder="请选择" clearable>
          <el-option
            v-for="item in escortList"
            :key="item.userId"
            :label="item.userName"
            :value="item.userId"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="remark" class="block">
        <label slot="label">备注</label>
        <el-input v-model="temp.remark" type="textarea" :rows="3" maxlength="250" show-word-limit />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        关闭
      </el-button>
      <el-button :loading="$loadingPlugin.add || $loadingPlugin.edit" type="primary" @click="success">
        保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { vehicleSettingAdd, queryAddVehicleWinDefault } from '@/api'
  import { mapGetters } from 'vuex'

  export default {
    name: 'add-setting',
    props: {
      vehicleList: {
        type: Array,
        required: true
      }
    },
    data() {
      return {
        visible: false,
        type: 'add',
        driverList: [],
        escortList: [],
        invenTruckInfo: {},
        temp: {
          truckCode: '',
          driverUserIds: [],
          escortUserIds: [],
          remark: ''
        },
        oldTruckCode: '',
        oldTruckId: 0,
        rules: {
          truckCode: [{ required: true, message: '请选择车辆', trigger: 'change' }],
          driverUserIds: [{ required: true, message: '请选择司机', trigger: 'blur' }],
          escortUserIds: [{ required: true, message: '请选择押运员', trigger: 'blur' }]
        }
      }
    },
    computed: {
      ...mapGetters([
        'orgList'
      ])
    },
    methods: {
      open(temp) {
        this.getList(temp ? temp.truckId : null)
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
          if (temp) {
            // this.temp = Object.assign(temp)
            this.oldTruckCode = temp.truckCode
            this.oldTruckId = temp.truckId
            this.type = 'edit'
          }
        })
      },
      async getList(truckId) {
        const res = await queryAddVehicleWinDefault(truckId ? { truckId } : null)
        this.driverList = JSON.parse(res.data.driverList)
        this.escortList = JSON.parse(res.data.escortList)
        this.invenTruckInfo = JSON.parse(res.data.invenTruckInfo)
        if (this.type === 'edit') this.temp = Object.assign(this.temp, this.invenTruckInfo)
        if (this.invenTruckInfo.driverList && this.invenTruckInfo.driverList.length > 0) this.temp.driverUserIds = this.invenTruckInfo.driverList.map(item => item.userId).filter(it => this.driverList.some(i => i.userId === it))
        if (this.invenTruckInfo.escortList && this.invenTruckInfo.escortList.length > 0) this.temp.escortUserIds = this.invenTruckInfo.escortList.map(item => item.userId).filter(it => this.escortList.some(i => i.userId === it))
      },
      success() {
        this.$refs.dataForm.validate(valid => {
          if (!valid) return
          const params = {
            truckCode: this.temp.truckCode,
            driverUserIds: this.temp.driverUserIds,
            escortUserIds: this.temp.escortUserIds,
            remark: this.temp.remark
          }
          params.driverUserIds = this.temp.driverUserIds.join(',') + ','
          params.driverUserNames = this.driverList.filter(item => this.temp.driverUserIds.some(it => it === item.userId)).map(i => i.userName).join(',') + ','
          params.escortUserIds = this.temp.escortUserIds.join(',') + ','
          params.escortUserNames = this.escortList.filter(item => this.temp.escortUserIds.some(it => it === item.userId)).map(i => i.userName).join(',') + ','
          params.oldTruckCode = this.oldTruckCode
          params.oldTruckId = this.oldTruckId
          if (this.type === 'add') {
            this.add(params)
          } else {
            this.edit(params)
          }
        })
      },
      async add(params) {
        console.log(params)
        await vehicleSettingAdd({ paramInfo: params })
        this.resolve()
        this.visible = false
        this.$notify({
          title: '提示',
          message: '新增成功',
          type: 'success',
          duration: 2000
        })
      },
      async edit(params) {
        await vehicleSettingAdd({ paramInfo: params })
        this.resolve()
        this.visible = false
        this.$notify({
          title: '提示',
          message: '更新成功',
          type: 'success',
          duration: 2000
        })
      },
      reset() {
        this.type = 'add'
        this.temp = {
          truckCode: '',
          driverUserIds: [],
          escortUserIds: [],
          remark: ''
        }
        this.$nextTick(() => {
          this.$refs.dataForm.clearValidate()
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
.form {
  display: flex;
  flex-wrap: wrap;
  & /deep/ .el-form-item {
    width: 50%;
    padding-right: 10px;
    .el-form-item__content > .el-select {
      width: 100%;
    }
  }
  .block {
    width: 100%;
  }
}
.info-title {
  display: flex;
  align-items: center;
  margin-top: 10px;
  margin-bottom: 20px;
  .title-icon {
    display: inline-block;
    width: 4px;
    height: 18px;
    margin-right: 6px;
    background: $themeColor;
  }
  span {
    font-weight: 700;
    color: $themeColor;
  }
}
</style>
